package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    /**
     * 保存或更新房间信息
     *
     * @param roomSubmitVo
     */
    @Override
    public void saveOrUpdateRoomInfo(RoomSubmitVo roomSubmitVo) {
        boolean isUpdate = roomSubmitVo.getId() != null;
        super.saveOrUpdate(roomSubmitVo);
        if (isUpdate){
            // 删除图片列表
            graphInfoService.remove(new LambdaUpdateWrapper<GraphInfo>().eq(GraphInfo::getItemId,roomSubmitVo.getId()).eq(GraphInfo::getItemType, ItemType.ROOM));
            // 删除属性信息列表
            roomAttrValueService.remove(new LambdaUpdateWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,roomSubmitVo.getId()));
            // 删除配套信息列表
            roomFacilityService.remove(new LambdaUpdateWrapper<RoomFacility>().eq(RoomFacility::getRoomId,roomSubmitVo.getId()));
            // 删除标签信息列表
            roomLabelService.remove(new LambdaUpdateWrapper<RoomLabel>().eq(RoomLabel::getRoomId,roomSubmitVo.getId()));
            // 删除支付方式列表
            roomPaymentTypeService.remove(new LambdaUpdateWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,roomSubmitVo.getId()));
            // 删除可选租期列表
            roomLeaseTermService.remove(new LambdaUpdateWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,roomSubmitVo.getId()));
        }
        // 插入图片列表
        if (!CollectionUtils.isEmpty(roomSubmitVo.getGraphVoList())){
            graphInfoService.saveBatch(roomSubmitVo.getGraphVoList().stream().map(graphVo -> {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                return graphInfo;
            }).collect(Collectors.toList()));
        }

        // 插入属性信息列表
        if (!CollectionUtils.isEmpty(roomSubmitVo.getAttrValueIds())){
        roomAttrValueService.saveBatch(roomSubmitVo.getAttrValueIds().stream().map(attrValueId -> {
            RoomAttrValue build = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attrValueId).build();
            return build;
        }).collect(Collectors.toList()));
        }

        // 插入配套信息列表
        if (!CollectionUtils.isEmpty(roomSubmitVo.getFacilityInfoIds())){
            roomFacilityService.saveBatch(roomSubmitVo.getFacilityInfoIds().stream().map(facilityInfoId -> {
                RoomFacility build = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(facilityInfoId).build();
                return build;
            }).collect(Collectors.toList()));
        }

        // 插入标签信息列表
        if (!CollectionUtils.isEmpty(roomSubmitVo.getLabelInfoIds())){
            roomLabelService.saveBatch(roomSubmitVo.getLabelInfoIds().stream().map(labelInfoId -> {
                RoomLabel build = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(labelInfoId).build();
                return build;
            }).collect(Collectors.toList()));
        }

        // 插入支付方式列表
        if (!CollectionUtils.isEmpty(roomSubmitVo.getPaymentTypeIds())){
            roomPaymentTypeService.saveBatch(roomSubmitVo.getPaymentTypeIds().stream().map(paymentTypeId -> {
                RoomPaymentType build = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(paymentTypeId).build();
                return build;
            }).collect(Collectors.toList()));
        }

        // 插入可选租期列表
        if (!CollectionUtils.isEmpty(roomSubmitVo.getLeaseTermIds())){
            roomLeaseTermService.saveBatch(roomSubmitVo.getLeaseTermIds().stream().map(leaseTermId -> {
                RoomLeaseTerm build = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(leaseTermId).build();
                return build;
            }).collect(Collectors.toList()));
        }
    }

    /**
     * 根据条件分页查询房间列表
     *
     * @param page
     * @param queryVo
     * @return
     */
    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageRoomItemByQuery(page,queryVo);
    }

    /**
     * 根据id获取房间详细信息
     *
     * @param id
     * @return
     */
    @Override
    public RoomDetailVo getDetailById(Long id) {
        //1.查询RoomInfo
        RoomInfo roomInfo = roomInfoMapper.selectById(id);

        //2.查询所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());

        //3.查询graphInfoList
        List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, id);

        //4.查询attrValueList
        List<AttrValueVo> attrvalueVoList = attrValueMapper.selectListByRoomId(id);

        //5.查询facilityInfoList
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByRoomId(id);

        //6.查询labelInfoList
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);

        //7.查询paymentTypeList
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);

        //8.查询leaseTermList
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);


        RoomDetailVo adminRoomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo, adminRoomDetailVo);

        adminRoomDetailVo.setApartmentInfo(apartmentInfo);
        adminRoomDetailVo.setGraphVoList(graphVoList);
        adminRoomDetailVo.setAttrValueVoList(attrvalueVoList);
        adminRoomDetailVo.setFacilityInfoList(facilityInfoList);
        adminRoomDetailVo.setLabelInfoList(labelInfoList);
        adminRoomDetailVo.setPaymentTypeList(paymentTypeList);
        adminRoomDetailVo.setLeaseTermList(leaseTermList);

        return adminRoomDetailVo;
    }

    /**
     * 根据id删除房间信息
     *
     * @param id
     */
    @Override
    public void removeRoomInfoById(Long id) {
        // 删除房间信息
        super.removeById(id);
        // 删除图片列表
        graphInfoService.remove(new LambdaUpdateWrapper<GraphInfo>().eq(GraphInfo::getItemId,id).eq(GraphInfo::getItemType, ItemType.ROOM));
        // 删除属性信息列表
        roomAttrValueService.remove(new LambdaUpdateWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId,id));
        // 删除配套信息列表
        roomFacilityService.remove(new LambdaUpdateWrapper<RoomFacility>().eq(RoomFacility::getRoomId,id));
        // 删除标签信息列表
        roomLabelService.remove(new LambdaUpdateWrapper<RoomLabel>().eq(RoomLabel::getRoomId,id));
        // 删除支付方式列表
        roomPaymentTypeService.remove(new LambdaUpdateWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId,id));
        // 删除可选租期列表
        roomLeaseTermService.remove(new LambdaUpdateWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId,id));
    }
}




